package network.step4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

//step3와 동일 - keyboard입력 -> 서버로 출력 -> 서버로 부터 입력 ->화면 출력
public class SimpleClient4 {
	public static void main(String[] args) throws IOException{
	
		Socket socket = null;
		PrintWriter out = null;
		BufferedReader br = null;
		BufferedReader key = null;
		
		try {
			socket = new Socket("127.0.0.1", 5000);
			System.out.println("============ 연결 완료 =============");
			
			System.out.println("==========입력을 시작하세요============");
			
			key = new BufferedReader(new InputStreamReader(System.in));
			//키보드를 통해 입력
			out = new PrintWriter(socket.getOutputStream(),true);
			//서버로 출력
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			//서버를 통해 문자열 입력
			
			String str = key.readLine();
			while(str != null){
				out.println(str);
				System.out.println(br.readLine());
				str = key.readLine();
			}
		}finally{
			if(out != null) out.close();
			if(br != null) br.close();
			if(key != null) key.close();
			if(socket != null) socket.close();
		}
		
	}
}
